var FeedOrderModel = function (feed, user, approvalUserIds) {
  BaseModel.call(this);
  var self = this;
  var fn = {
    initData: function (feed, approvalUserIds) {
      var fields = ['companyOrderId', 'factoryId', 'status', 'materialCode', 'materialType', 'queue', 'isEquals', 'materialName',
        'color', 'colorDescription', 'model', 'unit', 'feedCount', 'count', 'createTime', 'createUser', 'approvalTime', 'approvalUser',
        'detailId', 'providerName', 'isFeedUpdateOrder', 'productionUnitFeedCount', 'providerNo'
      ];
      self.setDataFromObj(feed, fields);

      var isFeedUpdateOrder = feed.isFeedUpdateOrder;
      if (isFeedUpdateOrder !== '0' && user.userId === feed.createUserId) {
        self.canUpdateOrder = true;
      } else {
        self.canUpdateOrder = false;
      }

      var notInCount = this.getFieldData(feed, "notInCount", 0);
      self.notInCount = notInCount;

      var relationCount = this.getFieldData(feed, "relationCount", 0);
      if (relationCount < 0) {
        relationCount = 0;
      }
      var feedUseCount = this.getFieldData(feed, "feedUseCount", 0);
      if (feedUseCount > 0) {
        relationCount += feedUseCount;
      } else {
        var materialUseCount = this.getFieldData(feed, 'materialUseCount', 0);
        relationCount += materialUseCount;
      }
      self.relationCount = relationCount;

      var count = this.getFieldData(feed, "count", 0);
      var receiveCount = count + notInCount - relationCount;
      if (receiveCount < 0) {
        receiveCount = 0;
      }
      receiveCount = _.round(receiveCount, 2);
      self.receiveCount = receiveCount;

      var purchaseCount;
      var feedCount = this.getFieldData(feed, 'feedCount', 0);
      if (feed.feedUseCount === null) {
        purchaseCount = feedCount - receiveCount;
      } else {
        purchaseCount = feedCount - feedUseCount;
      }
      if (purchaseCount < 0) {
        purchaseCount = 0;
      }
      purchaseCount = _.round(purchaseCount, 2);
      self.purchaseCount = purchaseCount;

      var createUserId = feed.createUserId;
      self.hasApprovalOrder = approvalUserIds.indexOf(createUserId) !== -1;

      if (feed.status === '1') {
        this.setBtns(feed);
      }
    },
    getFieldData: function (feed, field, defaultValue) {
      var value = _.get(feed, field, defaultValue);
      if (value === null) {
        return defaultValue;
      }
      return value;
    },
    setBtns: function (feed) {
      var specialRemark = feed.specialRemark;
      var isApperval = SysUtil.checkApproval(specialRemark, {
        lineNo: feed.queue,
        materialType: feed.materialType,
        materialFactory: feed.materialFactory,
        isOpraterAdd: feed.isOpraterAdd
      });
      self.isApperval = isApperval;
    }
  };

  fn.initData(feed, approvalUserIds);

  this.getClassName = function () {
    return 'FeedOrderModel';
  };
}